package server;

import java.io.InputStream;
import java.net.Socket;
import java.util.Map;

public class RequestProcessorr extends Thread {
    private Socket socket;
    private Map<String,HttpServlet> servletMap;


    public RequestProcessorr(Socket socket, Map<String, HttpServlet> servletMap) {
        this.socket = socket;
        this.servletMap = servletMap;
    }

    @Override
    public void run() {
        try {
            InputStream inputStream = socket.getInputStream();

            Request request = new Request(inputStream);
            Response response = new Response(socket.getOutputStream());
            HttpServlet httpServlet = servletMap.get(request.getUrl());
            if(httpServlet==null){

                response.outputHtml(request.getUrl());
            }else {
                httpServlet.service(request,response);
            }
            socket.close();

        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
